##################################
# loading data
import pandas as pd
data = pd.read_csv('E:/wochong/s40. MetaCell/sankey/draw.txt',sep='\t',header=0)
data['label'] = data.apply(lambda row : row['From'].split('_')[1],axis=1)
data['time'] = data.apply(lambda row : row['From'].split('_')[0],axis=1)
data.head()
| From | To | number | label | time | |
|---|---|---|---|---|---|
| 0 | WT_other | 0hpa1_epidermal | 2937 | other | WT |
| 1 | WT_other | 0hpa1_pharynx | 9 | other | WT |
| 2 | WT_other | 0hpa1_protonephridia | 26 | other | WT |
| 3 | WT_other | 0hpa1_neural | 1390 | other | WT |
| 4 | WT_other | 0hpa1_cathepsin | 2433 | other | WT |
data['label'].unique()
array(['other', 'Nb2', 'cathepsin', 'epidermal', 'gut', 'muscle',
'neural', 'parenchymal', 'protonephridia', 'pharynx'], dtype=object)
############################
# config x.y,color
import numpy as np
X_pos = {
'WT' : 0.01,
'0hpa1' : 0.06,
'12hpa2': 0.13,
'36hpa2': 0.26,
'3dpa2' : 0.39,
'5dpa1' : 0.51,
'7dpa2' : 0.64,
'10dpa1': 0.77,
'14dpa1': 0.9
}
Depths = {
'0hpa1' : 0,
'12hpa2': 1,
'36hpa2': 2,
'3dpa2' : 3,
'5dpa1' : 4,
'7dpa2' : 5,
'10dpa1': 6,
'14dpa1': 7
}
colors = [
'#1EA51D',
'#FBD26A',
'#002FA7',
'#BB86FC',
'#03DAC6',
'#CF6679',
'#A51DA5',
'#B0B0B0',
'#D62237',
'#333333',
]
Y_pos = {}
Cmap = {}
for i,x in enumerate(data['label'].unique()):
Y_pos[x] = float(i)/10.0
Cmap[x] = colors[i]
Cmap
{'other': '#1EA51D',
'Nb2': '#FBD26A',
'cathepsin': '#002FA7',
'epidermal': '#BB86FC',
'gut': '#03DAC6',
'muscle': '#CF6679',
'neural': '#A51DA5',
'parenchymal': '#B0B0B0',
'protonephridia': '#D62237',
'pharynx': '#333333'}
Y_pos
{'other': 0.0,
'Nb2': 0.1,
'cathepsin': 0.2,
'epidermal': 0.3,
'gut': 0.4,
'muscle': 0.5,
'neural': 0.6,
'parenchymal': 0.7,
'protonephridia': 0.8,
'pharynx': 0.9}
Y_pos = {
'cathepsin': 0.1,
'gut': 0.2,
'epidermal': 0.3,
'muscle': 0.4,
'neural': 0.5,
'Nb2': 0.6,
'parenchymal': 0.7,
'protonephridia': 0.8,
'pharynx': 0.9,
'other':0.91,
}
Cmap = { 'other':'#000000',
'neural': '#BB86FC',
'cathepsin': '#03DAC6',
'epidermal': '#002FA7',
'gut': '#1EA51D',
'muscle': '#CF6679',
'Nb2': '#D62237',
'parenchymal': '#FBD26A',
'pharynx':'#B0B0B0' ,
'protonephridia': '#A51DA5'}
import numpy as np
def get_nodes_links(data):
node1 = data['From'].unique()
node2 = data['To'].unique()
nodes = np.union1d(node1,node2)
node_id = {}
i = 0;
node = {
"label":[],
"x":[],
"y":[],
"color":[],
};
link = {
"source": [],
"target": [],
"value": [],
"color" :[],
}
#create nodes
for x in nodes:
time = x.split('_')[0]
anno = x.split('_')[1]
node_id[x] = i;
i = i + 1 ;
node['label'].append("")
node['x'].append(X_pos[time])
node['y'].append(Y_pos[anno])
node['color'].append(Cmap[anno])
#create links
for _ , row in data.iterrows():
anno = row['label']
fromkey = row['From']
tokey= row['To']
link['source'].append(node_id[fromkey])
link['target'].append(node_id[tokey])
link['value'].append(row['number'])
link['color'].append(Cmap[anno])
return node,link
import plotly.graph_objects as go
import plotly.io as pio
pio.kaleido.scope.default_format = "svg"
nodes , links = get_nodes_links(data)
fig = go.Figure(go.Sankey(node = nodes,arrangement='perpendicular',
link = links ) )
fig.update_layout(
title="Test",
font=dict(size = 10, color = 'white'),
plot_bgcolor='black',
paper_bgcolor='black'
)
fig.update_layout(
autosize=False,
width=700,
height=700,
)
fig.show()
import kaleido
fig.write_image("E:/wochong/fig1.pdf")
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_16716\355650303.py in <module> ----> 1 import kaleido 2 fig.write_image("E:/wochong/fig1.pdf") ModuleNotFoundError: No module named 'kaleido'
import plotly.graph_objects as go
nodes , links = get_nodes_links(data)
fig = go.Figure(go.Sankey(node = nodes,arrangement='perpendicular',
link = links ) )
fig.update_layout(
title="Test",
font=dict(size = 10, color = 'white'),
plot_bgcolor='black',
paper_bgcolor='black'
)
fig.update_layout(
autosize=False,
width=1000,
height=700,
)
fig.show()
fig.write_image("E:/wochong/fig1.pdf")
pip install -U kaleido
Defaulting to user installation because normal site-packages is not writeable Collecting kaleido Using cached kaleido-0.2.1-py2.py3-none-win_amd64.whl (65.9 MB) Installing collected packages: kaleido Successfully installed kaleido-0.2.1 Note: you may need to restart the kernel to use updated packages.
fig.write_image("E:/wochong/fig1.png")
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-13-1f9aee066174> in <module> ----> 1 fig.write_image("E:/wochong/fig1.png") D:\Anaconda\lib\site-packages\plotly\basedatatypes.py in write_image(self, *args, **kwargs) 3827 import plotly.io as pio 3828 -> 3829 return pio.write_image(self, *args, **kwargs) 3830 3831 # Static helpers D:\Anaconda\lib\site-packages\plotly\io\_kaleido.py in write_image(fig, file, format, scale, width, height, validate, engine) 272 height=height, 273 validate=validate, --> 274 engine=engine, 275 ) 276 D:\Anaconda\lib\site-packages\plotly\io\_kaleido.py in to_image(fig, format, width, height, scale, validate, engine) 136 which can be installed using pip: 137 $ pip install -U kaleido --> 138 """ 139 ) 140 ValueError: Image export using the "kaleido" engine requires the kaleido package, which can be installed using pip: $ pip install -U kaleido